/*****************************************************************************
 * p300.c
 *
 * Copyright (C) 2020 Jeasonvor xq1101627@163.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/

#include "plum_api.h"
#include "thirdparty/modbus/modbus.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_ERROR
#define LOG_MODULE    "sensor.p300"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
#define DEV_SENSOR_P300_PRESS_IDX 0

/*****************************************************************************
 * Private Types
 ****************************************************************************/
typedef struct {
    plum_u16 addr;
    plum_u16 value;
} press_register_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

PLUM_PRIVATE press_register_t press_tb[] = {
    [DEV_SENSOR_P300_PRESS_IDX] = {0x0004, 0}};

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

plum_void dev_sensor_p300_reg_notice(modbus_protocol_evt evt, plum_void *arg)
{
    switch (evt) {
        case MODBUS_EVT_MSG:
        {
            modbus_msg_t *msg = (modbus_msg_t *)arg;
            if (msg->addr == press_tb[DEV_SENSOR_P300_PRESS_IDX].addr) {
                press_tb[DEV_SENSOR_P300_PRESS_IDX].value =
                    *(plum_u16 *)msg->value;
            }
        } break;

        default:
            LOG_E("not support the event : %d", evt);
            break;
    }
}
/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 dev_press_sensor_init(
    int (*io_read)(unsigned char *data, unsigned int len, unsigned int timeout),
    int (*io_write)(unsigned char *data, unsigned int len))
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        PLUM_UNUSED(io_read);
        PLUM_UNUSED(io_write);
    } while (0);

    return (rc);
}

float dev_press_sensor_read(unsigned char id)
{
    do {
        plum_s32 rc = PLUM_ECODE_OK;

        rc = modbus_protocol_register_notice(dev_sensor_p300_reg_notice);
        if (rc) {
            LOG_E("modbus_protocol_register_notice err,rc: %d", rc);
            break;
        }

        rc = modbus_protocol_read(MODBUS_PROTOCOL_RTU, id,
                                  MODBUS_PROTOCOL_READ_SHOLD,
                                  press_tb[DEV_SENSOR_P300_PRESS_IDX].addr, 1);
        if (rc) {
            LOG_E("modbus_protocol_read err,rc: %d", rc);
            break;
        }

    } while (0);

    return (press_tb[DEV_SENSOR_P300_PRESS_IDX].value);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
